package projectmanagement.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

@Entity
public class Project implements Serializable {

	private static final long serialVersionUID = 1;

	@Id
	@GeneratedValue
	private int id;

	@OneToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "Project_employees",
			joinColumns = @JoinColumn(name = "project"),
			inverseJoinColumns = @JoinColumn(name = "employee"))
	private Set<Person> persons;
	
	@Transient
	private String[] personsStr;
	
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<Person> getPersons() {
		return persons;
	}

	public void setPersons(Set<Person> persons) {
		this.persons = persons;
	}

	public String[] getPersonsStr() { 
		if(persons != null){
			personsStr = new String[persons.size()];
			
			Iterator<Person> ip = persons.iterator();
			for(int i = 0; ip.hasNext(); i++){
				personsStr[i] = String.valueOf(ip.next().getId());
			}
		}
		return personsStr;
	}

	public void setPersonsStr(String[] personsStr) {
		persons = new HashSet<Person>();
		
		Person person;
		for (int i = 0; i < personsStr.length; i++){
			person = new Person();
			person.setId(Integer.parseInt(personsStr[i]));
			persons.add(person);
		}
	}
}
